  <?php 
  $repository=BO::getProductBORepository();
  $id=getFormValue('id');
  $productBO = $repository->getProductBO($id);	
  
	function render_tree_open($category,$selectedCategories)
	{
		
		$subCategories = $category->getSubCategories();
		
		if(count($subCategories) === 0)
		{
			if(isset($selectedCategories))
			{
				$id=$category->getID();			
				if (array_key_exists($id, $selectedCategories)) 				
					render_tree_leaf($category,true);
				else
					render_tree_leaf($category,false);
			}
			else
			{
				render_tree_leaf($category,false);
			}
		}
		else
		{
			echo ' 	<li>';
			echo '	<div class="btn-group">';
			echo '		<a href="dispatch.php?target=category-edit&id='.$category->getID().'"><span class="glyphicon glyphicon-minus">'.$category->getName().'</span></a>';		  			
			echo ' </div>';
			echo '		<ul>';
			foreach($subCategories as $subCategory)		
				render_tree_open($subCategory,$selectedCategories);
			echo '		</ul>';
			echo '	</li>';
		}
		
	}
	
	function render_tree_leaf($category,$selected=false)
	{
			echo '<li><div class="btn-group">
					<div>
					<label class="checkbox-inline">';
		if($selected)			
			echo	 '<input type="checkbox" name="category_ids[]" checked="checked" value="'.$category->getID().'">'.$category->getName();
		else
			echo	'<input type="checkbox" name="category_ids[]" value="'.$category->getID().'">'.$category->getName();
			echo            '</label>
					</div>
				</div></li>';
	}
	
?>
<div class="panel panel-default">
  <!-- Default panel contents -->
  <div class="panel-heading">Product : <?php echo $productBO->getName() ?></div>
  <div class="panel-body">
		<p>
		
		</p>	

	  	
		<ul class="nav nav-tabs" role="tablist">
		  <li><a href="dispatch.php?target=product-edit&id=<?php echo $productBO->getID() ?>">General</a></li>
		  <li><a href="dispatch.php?target=product_attribute-edit&id=<?php echo $productBO->getID() ?>">Attributes</a></li>
		  <li><a href="dispatch.php?target=product_images-edit&id=<?php echo $productBO->getID() ?>">Images</a></li>
		  <li><a href="dispatch.php?target=product_pricing-edit&id=<?php echo $productBO->getID() ?>">Pricing</a></li>
		  <li class="active"><a href="dispatch.php?target=product_categories-edit&id=<?php echo $productBO->getID() ?>">Categories</a></li>
		  <li><a href="dispatch.php?target=product_variations-edit&id=<?php echo $productBO->getID() ?>">Variations</a></li>
		</ul>
		
		<div class="tree well">
			<h1>Product : Categories</h1>
			<form class="form-horizontal" action="dispatch.php" method="post">
				<input type="hidden" name="target" value="product_categories-save">
				<input type="hidden" name="id" value="<?php echo $productBO->getID() ?>">
      
			<?php 
				$categoryrepository=BO::getCategoryBORepository();
				$categoryBOs = $categoryrepository->getRootCategoryBOs(); 
				$selectedCategories=$productBO->getCategories();
				foreach($categoryBOs as $categoryBO)
				 {
					echo '<ul>';
						render_tree_open($categoryBO,$selectedCategories);
					echo '</ul>';
				 }
			?>
			<br>
			<div class="form-group">
				<div class="col-xs-offset-3 col-xs-9">
					<input type="submit" class="btn btn-primary" value="Submit">
					<input type="reset" class="btn btn-default" value="Reset">
				</div>
			</div>
			</form>	
		</div>		  
		 <link href="assets/bootstrap_extn/css/tree.css" rel="stylesheet">
		<script src="assets/bootstrap_extn/js/tree.js"></script>
	</div>
	
	 <div class="panel-footer">
		<div class="btn-group">
			<form action="dispatch.php" method="post">
				<button type="submit" name="target" value="product-add" class="btn btn-default btn-lg">
					<span class="glyphicon glyphicon-plus"></span> New Product
				</button>
			</form>
		</div>
	</div>	
</div>	
		  
		  
		  
